<template>
  <div class="app-container">
    <!-- 添加章节按钮 -->
    <div>
      <el-button type="primary" @click="addChapter">添加章节</el-button>
    </div>

    <!--章节列表-->
    <ul class="chapterList">
      <li v-for="chapter in chapterList"
                :key="chapter.id">
          <p>
            {{chapter.title}}
            <span class="acts">
                <el-button type="text" @click="addVideo(chapter.id)">添加课时</el-button>
                <el-button type="text" @click="editChapter(chapter.id)">编辑</el-button>
                <el-button type="text" @click="removeChapterById(chapter.id)">删除</el-button>
            </span>
          </p>
        <!-- 视频 -->
        <ul class="chapterList videoList">
          <li
            v-for="video in chapter.children"
            :key="video.id">
            <p>
              {{ video.title }}
              <el-tag v-if="!video.videoSourceId" size="mini" type="danger">
                {{ '尚未上传视频' }}
              </el-tag>
              <span class="acts">
                        <el-tag v-if="video.free" size="mini" type="success">{{ '免费观看' }}</el-tag>
                        <el-button type="text" @click="eiitVideo(chapter.id,video.id)">编辑</el-button>
                        <el-button type="text" @click="removeVideo(video.id)">删除</el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>
    <!--添加章节的弹窗-->
    <chapter-form ref="chapterForm"/>
    <!--添加课时的弹窗-->
    <video-form ref="videoForm"/>

    <div style="text-align: center">
      <el-button type="primary" @click="prev()">上一步</el-button>
      <el-button type="primary" @click="next()">下一步</el-button>
    </div>
  </div>
</template>

<script>
// 引入组件
import ChapterForm from '@/views/course/components/Chapter/Form'
import VideoForm from '@/views/course/components/Video/Form'
import videoApi from '@/api/video'
import vodApi from '@/api/vod'
import chapterApi from '@/api/chapter'
import course from '@/api/course'
export default {
  name: "index",
  // 注册组件
  components:{ChapterForm,VideoForm},
  data() {
    return {
      chapterList: [], //嵌套的章节信息
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      chapterApi.getNestedTreeList(this.$parent.courseId).then(res => {
        this.chapterList = res.data.items
      })
    },
    prev() {
      this.$parent.active = '0'
    },
    next() {
      this.$parent.active = '2'
    },
    removeChapterById(id) {
      this.$confirm('此操作将永久删除该章节，是否继续?', '提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        chapterApi.removeById(id).then(res => {
          this.$message.success(res.message)
          this.fetchData()
        })
      }).catch(res => {
        if (res == 'cancel') {
          this.$message.info('取消删除')
        }
      })
    },
    //打开弹窗
    addChapter() {
      this.$refs.chapterForm.open()
    },
    editChapter(id) {
      //打开弹窗，回显数据
      this.addChapter()
      chapterApi.getById(id).then(res => {
        this.$refs.chapterForm.chapter = res.data.item
      })
    },
    addVideo(chapterId) {
      this.$refs.videoForm.open(chapterId)
    },
    eiitVideo(chapterId,videoId) {
      this.$refs.videoForm.open(chapterId,videoId)
    },
    removeVideo(videoId) {
      this.$confirm('此操作将永久删除该课时，是否继续?', '提示',{
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
        videoApi.removeById(videoId).then(res => {
          this.$message.success(res.message)
          this.fetchData()
        })
      }).catch(res => {
        if (res == 'cancel') {
          this.$message.info('取消删除')
        }
      })
    }

  }
}
</script>

<style scoped>
.chapterList{
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chapterList li{
  position: relative;
}
.chapterList p{
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}
.chapterList .acts {
  float: right;
  font-size: 14px;
}

.videoList{
  padding-left: 50px;
}

.videoList p{
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dashed #DDD;
}
</style>
